package wechat.rcv.cosuming;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import windbell.domain.user.BaseUser;
import windbell.domain.user.WechatService;

//import com.industrialpark.domain.WeChatUserDAO;

@Component
public class EventUnSubscribeConsuming implements IMsgConsuming {
	
	private RequestObj req;
	
//	@Autowired
//	private WeChatUserDAO weChatUserDAO;
	
	@Autowired
	@Qualifier("windbell.domain.user.WechatService")
	private WechatService wechatService;
	
	final static Logger logger = Logger.getLogger(EventUnSubscribeConsuming.class.getName());

	@Override
	public String acceptSync() {
		try {
			logger.info("******EventUnSubscribeConsuming*****openid "+req.getFromUserName());
			BaseUser user = wechatService.getUserbyWechatId(req.getFromUserName());
			assertNotNull(user);
			wechatService.unregisterWeChatUser(req.getFromUserName(), user);
//			assertNotNull(weChatUserDAO.findActiveWeChatUser(req.getFromUserName())).getCompany().unregisterWeChatUser(req.getFromUserName());	
		}
		catch (RuntimeException e){
			logger.info("=======================>EventUnSubscribeConsuming Exception: " + e.toString());
//			e.printStackTrace();
		}
		return "";
	}

	@Override
	public String acceptAsync() {
		return acceptSync();
	}
	@Override
	public void setRequestObj(RequestObj ro) {
		// TODO Auto-generated method stub
		req = ro;
	}
	private <T> T assertNotNull(T t){
		if(t==null) throw new RuntimeException();
		return t;
	}
}
